$def with (items, key, sponsorship_count=None, reading_log_counts=None, lists=None, user=None, logged_in_user=None, public=False)
$# Displays a user's reading log
$# :param list items:
$# :param Literal['currently-reading', 'want-to-read', 'already-read', 'sponsorships', 'loans', 'waitlist'] key:
$# :param int? sponsorship_count: number of books the user is sponsoring
$# :param Dict[Literal['currently-reading', 'want-to-read', 'already-read'], int] reading_log_counts:
$# :param list? lists:
$# :param user:
$# :param bool public:

$ title = _('My Books')
$ urlbase = ctx.path.rsplit('/', 1)[0]
$ username = user.key.split('/')[-1]
$ owners_page = logged_in_user and urlbase.startswith('/people/'+logged_in_user.key.split('/')[-1])
$ meta_photo_url = "https://archive.org/services/img/%s" % get_internet_archive_id(user.key)

$ current_page = int(input(page=1).page)
$ total_items = sponsorship_count if key == 'sponsorships' else int(reading_log_counts[key])
$ userDisplayName = user.displayname or ctx.user.displayname
$ userKey = user.key or ctx.user.key

$ header_title = unicode(render_template('account/readinglog_shelf_name', key)).strip()
$if key == 'currently-reading':
  $ og_title = _("Books %(username)s is reading", username=userDisplayName)
  $ og_description = _("%(username)s is reading %(total)d books. Join %(username)s on OpenLibrary.org and tell the world what you're reading.", username=userDisplayName, total=total_items)
$elif key == 'want-to-read':
  $ og_title = _("Books %(username)s wants to read", username=userDisplayName)
  $ og_description = _("%(username)s wants to read %(total)d books. Join %(username)s on OpenLibrary.org and share the books that you'll soon be reading!", username=userDisplayName, total=total_items)
$elif key == 'already-read':
  $ og_title = _("Books %(username)s has read", username=userDisplayName)
  $ og_description = _("%(username)s has read %(total)d books. Join %(username)s on OpenLibrary.org and tell the world about the books that you care about.", username=userDisplayName, total=total_items)
$elif key == 'sponsorships':
  $ header_title = _("Sponsorships")
  $ og_title = _("Books %(userdisplayname)s is sponsoring", userdisplayname=userDisplayName)
  $ og_description = "{username} is sponsoring {total} books. Join {username} on OpenLibrary.org and share the books that you'll soon be reading!".format(username=userDisplayName, total=total_items)

$if owners_page:
  $var title: $header_title
$else:
  $var title: $userDisplayName &raquo; $header_title

$if og_title:
  $putctx("description", og_description)
  $add_metatag(property="og:title", content=og_title)
  $add_metatag(property="og:url", content=request.canonical_url)
  $add_metatag(property="og:site_name", content="Open Library")
  $add_metatag(property="og:description", content=og_description)
  $add_metatag(property="og:image", content=meta_photo_url)

<div id="contentHead">
  <div id="mybooks">
    <div class="small sansserif grey account-settings-menu">
      <a href="$userKey">$userDisplayName</a> &raquo; $_("Reading Log")
    </div>
    <div>

    </div>
    <div>
      <h1 style="display:inline">
        $if not owners_page:
          $userDisplayName &raquo; $header_title
        $else:
          $header_title
      </h1>

      $if owners_page:
        <a href="$ctx.path/stats" title="$('View stats about this shelf')">$_('Stats')</a>
    </div>

    $if owners_page:
      <h3>
        $if public:
          $_('Your reading log is currently set to public')
          <button onclick="prompt('Copy share link to clipboard:', window.location.protocol + '//' + window.location.hostname + '/people/$(username)/books/$(key)');">Share</button> or
        $else:
          $_('Your reading log is currently set to private')
        &mdash;
        <a href="/account/privacy">$_('Manage your privacy settings')</a>
      </h3>

    <div class="mybooks">
      <div class="mybooks-menu">
        <ul class="preset-lists">
          <li class="subsection">$_('Reading Log')</li>
          <li><a href="$urlbase/currently-reading" $('class=selected' if key == 'currently-reading' else '')>$_('Currently Reading (%(count)d)', count=reading_log_counts['currently-reading'])</a></li>
          <li><a href="$urlbase/want-to-read" $('class=selected' if key == 'want-to-read' else '')>$_('Want to Read (%(count)d)', count=reading_log_counts['want-to-read'])</a></li>
          <li><a href="$urlbase/already-read" $('class=selected' if key == 'already-read' else '')>$_('Already Read (%(count)d)', count=reading_log_counts['already-read'])</a></li>
        </ul>
        $if owners_page and ctx.user and ctx.user.in_sponsorship_beta():
          <!-- only show to owners -->
          <ul class="preset-lists">
            <li class="subsection">$_('Sponsorships')</li>
            <li><a href="$urlbase/sponsorships" $('class=selected' if key == 'sponsorships' else '')>Sponsoring ($sponsorship_count)</a></li>
          </ul>
        $if lists:
          <ul class="user-lists">
            <li class="subsection">$_('Lists')</li>
            <div class="list-overflow">
              $for lst in lists:
                <li><a class="list-link" href="$(lst.key)">$lst['name'] ($(len(lst.seeds)))</a></li>
            </div>
          </ul>
      </div>
      <div class="mybooks-list">
        $:macros.Pager(current_page, total_items, results_per_page=25)
        <ul class="list-books" id="siteSearch">
          $if items:
            $ bookshelf_id = {'want-to-read': 1, 'currently-reading': 2, 'already-read': 3}.get(key, None)
            $for item in items:
              $if key == 'loans':
                $ edition = get_document(item['book'])
                $ work = get_document(edition.works[0]) if (edition.get('type', {}).get('key') == '/type/work' and edition.works and edition.works[0]) else None
                $ decorations = owners_page and macros.ManageLoansButtons(item, edition) or ''
                $:macros.SearchResultsWork(work or edition, decorations=decorations, cta=False)
              $elif key == 'waitlists':
                $ edition = item
                $ decorations = owners_page and macros.ManageWaitlistButton(edition) or ''
                $:macros.SearchResultsWork(edition, decorations=decorations, cta=False)
              $else:
                $ work = item
                $ decorations = (bookshelf_id and owners_page) and macros.ReadingLogButton(work, read_status=bookshelf_id)
                $:macros.SearchResultsWork(work, decorations=decorations, cta=False)
          $else:
            <li>$_('No books are on this shelf')</li>
        </ul>
        $:macros.Pager(current_page, total_items, results_per_page=25)
      </div>
    </div>
  </div>
</div>
